home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2009 February / PCWFEB09.iso / Software / Resources / Chat & Communication / Digsby build 37 / digsby_setup.exe / lib / util / command_queue.pyo (.txt) < prev    next >
Python Compiled Bytecode  |  2008-10-13  |  5KB  |  192 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyo (Python 2.5)
  3.  
  4. from __future__ import with_statement
  5. from util import do, Timer, threaded
  6. from Queue import Queue
  7. from threading import RLock
  8. from functools import wraps
  9. from util import callsback, WorkRequest, ThreadPool, callany
  10. from Queue import Empty
  11. import logging
  12. log = logging.getLogger('cmdq')
  13.  
  14. class CommandQueue(object):
  15.     
  16.     def __init__(self, start_hooks, end_hooks, shutdown_interval = 30, retry_time = 2):
  17.         self.start_hooks = start_hooks
  18.         self.end_hooks = end_hooks
  19.         self.shutdown_interval = shutdown_interval
  20.         self.retry_time = retry_time
  21.         self.do_queue = Queue()
  22.         self.state_lock = RLock()
  23.         self.timer = Timer(shutdown_interval, self.finish)
  24.         self.loop_running = False
  25.         self.initialized = False
  26.         self.initializing = False
  27.         self.shutting_down = False
  28.         self.timer_valid = False
  29.  
  30.     
  31.     def add_new(self, .1):
  32.         (func, instance, a, k) = .1
  33.         self.state_lock.__enter__()
  34.         
  35.         try:
  36.             self.timer_valid = False
  37.             self.timer.cancel()
  38.             self.do_queue.put((func, instance, a, k))
  39.             if not self.loop_running:
  40.                 self.loop_running = True
  41.                 if self.initialized:
  42.                     self.flush()
  43.                 elif not self.initializing:
  44.                     self.initializing = True
  45.                     self.initialize()
  46.                 
  47.         finally:
  48.             pass
  49.  
  50.  
  51.     
  52.     def initialize(self):
  53.         self.state_lock.__enter__()
  54.         
  55.         try:
  56.             if self.shutting_down:
  57.                 t = Timer(self.retry_time, self.initialize)
  58.                 t.start()
  59.                 return None
  60.         finally:
  61.             pass
  62.  
  63.         
  64.         try:
  65.             do((lambda .0: for f in .0:
  66. f())(self.start_hooks))
  67.         except Exception:
  68.             self.state_lock
  69.             e = self.state_lock
  70.             import traceback as traceback
  71.             traceback.print_exc()
  72.             self.state_lock.__enter__()
  73.             
  74.             try:
  75.                 self.do_queue = Queue()
  76.                 self.loop_running = False
  77.                 self.initialized = False
  78.                 self.initializing = False
  79.                 return None
  80.             finally:
  81.                 pass
  82.  
  83.         except:
  84.             self.state_lock
  85.  
  86.         self.state_lock.__enter__()
  87.         
  88.         try:
  89.             self.initialized = True
  90.             self.initializing = False
  91.         finally:
  92.             pass
  93.  
  94.         self.flush()
  95.  
  96.     initialize = threaded(initialize)
  97.     
  98.     def flush(self):
  99.         self.state_lock.acquire()
  100.         while True:
  101.             self.state_lock.release()
  102.             
  103.             try:
  104.                 (func, instance, a, k) = self.do_queue.get(False)
  105.             except Empty:
  106.                 break
  107.             else:
  108.                 func(instance, *a, **k)
  109.             finally:
  110.                 self.state_lock.acquire()
  111.  
  112.         self.loop_running = False
  113.         self.timer_valid = True
  114.         self.timer.start()
  115.         self.state_lock.release()
  116.  
  117.     flush = threaded(flush)
  118.     
  119.     def finish(self):
  120.         self.state_lock.__enter__()
  121.         
  122.         try:
  123.             if not self.timer_valid:
  124.                 return None
  125.             
  126.             self.initialized = False
  127.             self.shutting_down = True
  128.         finally:
  129.             pass
  130.  
  131.         do((lambda .0: for f in .0:
  132. f())(self.end_hooks))
  133.         self.state_lock.__enter__()
  134.         
  135.         try:
  136.             self.shutting_down = False
  137.         finally:
  138.             pass
  139.  
  140.  
  141.     finish = threaded(finish)
  142.  
  143.  
  144. def cmdqueue(qname = 'cmdq'):
  145.     
  146.     def wrapper2(func):
  147.         
  148.         def wrapper1(instance, *args, **kws):
  149.             print instance, args, kws
  150.             cmdq = getattr(instance, qname)
  151.             cmdq.add_new((func, instance, args, kws))
  152.  
  153.         return wrapper1
  154.  
  155.     return wrapper2
  156.  
  157.  
  158. def callback_cmdqueue(qname = 'cmdq'):
  159.     
  160.     def wrapper2(func):
  161.         
  162.         def wrapper(instance, callback = (callsback, None), *args, **kws):
  163.             requestID = kws.pop('requestID', None)
  164.             cmdq = getattr(instance, qname)
  165.             
  166.             def do_thing(*a, **k):
  167.                 
  168.                 try:
  169.                     result = func(instance, *args, **kws)
  170.                     exception = None
  171.                 except Exception:
  172.                     e = None
  173.                     exception = e
  174.                     result = None
  175.                     import traceback
  176.                     import sys as sys
  177.                     sys.stderr.write('The following exception occurred in callback_cmdqueue:\n')
  178.                     traceback.print_exc()
  179.  
  180.                 if exception:
  181.                     callany(callback.error, e)
  182.                 else:
  183.                     callany(callback.success, result)
  184.  
  185.             cmdq.add_new((do_thing, None, (), { }))
  186.  
  187.         wrapper = None(wraps(func)(wrapper))
  188.         return wrapper
  189.  
  190.     return wrapper2
  191.  
  192.